﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.IO;
using System;
using System.ComponentModel;

namespace BDSim
{
    /// <summary>
    /// FileSaveWnd.xaml 的交互逻辑
    /// </summary>
    public partial class FileSaveWnd : UserControl, INotifyPropertyChanged, ILayoutContent
    {
        public FileSaveWnd()
        {
            InitializeComponent();
            CreatDataNode();
        }

        #region///委托
        public delegate void StartSaveThread(ObservableCollection<DataSaveFlagModel> dataSaveFlagModelList, DataSaveType saveType, string path);

        public StartSaveThread DlgStartSaveThread;
        #endregion

        #region ///属性，成员
        ObservableCollection<DataSaveFlagModel> _DataSaveFlagModelList = new ObservableCollection<DataSaveFlagModel>();

        public ObservableCollection<DataSaveFlagModel> DataSaveFlagModelList
        {
            get { return _DataSaveFlagModelList; }
            set { _DataSaveFlagModelList = value; }
        }

        private ObservableCollection<DataSaveType> _DataSaveTypeList = new ObservableCollection<DataSaveType>() 
        {
            {DataSaveType.Rinex3_02格式},
           // {DataSaveType.Rinex2_10格式},
          //  {DataSaveType.自定义格式}
        };

        public ObservableCollection<DataSaveType> DataSaveTypeList
        {
            get { return _DataSaveTypeList; }
            set { _DataSaveTypeList = value; NotifyPropertyChanged("DataSaveTypeList"); }
        }

        private DataSaveType _SelectedDataSaveType;//= DataSaveType.数据文本格式;

        public DataSaveType SelectedDataSaveType
        {
            get { return _SelectedDataSaveType; }
            set { _SelectedDataSaveType = value; }
        }

        //仿真数据保存 导航系统选择
        //private List<NavSysType> _NavSysTypeList = new List<NavSysType>()
        //{
        //    {NavSysType.Beidou系统},{NavSysType.GPS系统},{NavSysType.Galileo系统},{NavSysType.GLONASS系统},{NavSysType.其它}
        //};

        //public List<NavSysType> NavSysTypeList
        //{
        //    get { return _NavSysTypeList; }
        //    set { _NavSysTypeList = value; }
        //}

        //private NavSysType _SelectedNavSysType;//= NavSysType.Beidou系统;

        //public NavSysType SelectedNavSysType
        //{
        //    get { return _SelectedNavSysType; }
        //    set { _SelectedNavSysType = value; }
        //}

        private string _dataSavePath = Directory.GetCurrentDirectory() +"\\DataRecords";

        public string DataSavePath
        {
            get { return _dataSavePath; }
            set { _dataSavePath = value; }
        }

        #endregion

        #region ///内部私有方法

        public void CreatDataNode()
        {
            DataSaveFlagModel spaceDataSave = new DataSaveFlagModel(){Name = "空间段"};
            DataSaveFlagModel orbitData = new DataSaveFlagModel() { Name = "轨道数据"};
            DataSaveFlagModel clkSpaceData = new DataSaveFlagModel() { Name = "钟差数据" };
            spaceDataSave.SubModelList.Add(orbitData);
            spaceDataSave.SubModelList.Add(clkSpaceData);

            DataSaveFlagModel groundDataSave = new DataSaveFlagModel() { Name = "地面控制段"};
            DataSaveFlagModel navData = new DataSaveFlagModel() { Name = "导航电文" };
            DataSaveFlagModel downObsData = new DataSaveFlagModel() { Name = "观测数据" };
            DataSaveFlagModel clkGroundData = new DataSaveFlagModel() { Name = "钟差数据" };
            groundDataSave.SubModelList.Add(navData);
            groundDataSave.SubModelList.Add(downObsData);
            groundDataSave.SubModelList.Add(clkGroundData);


            DataSaveFlagModel userDataSave = new DataSaveFlagModel() { Name = "用户段" };
            DataSaveFlagModel userObsdata = new DataSaveFlagModel() { Name = "观测数据" };
            //DataSaveFlagModel trackdata = new DataSaveFlagModel() { Name = "轨迹数据" };
            //DataSaveFlagModel ppositiondata = new DataSaveFlagModel() { Name = "定位数据" };
            userDataSave.SubModelList.Add(userObsdata);
            //userDataSave.SubModelList.Add(trackdata);
            //userDataSave.SubModelList.Add(ppositiondata);

            DataSaveFlagModelList.Add(spaceDataSave);
            DataSaveFlagModelList.Add(groundDataSave);
            DataSaveFlagModelList.Add(userDataSave);
        }

        #endregion

        #region 保存数据按钮事件
        /// <summary>
        /// 保存数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveData_Click(object sender, RoutedEventArgs e)
        {
            switch (BDSimObject.GlobalStatus)
            {
                case ESimStatus.UnInited:
                case ESimStatus.Initing:
                case ESimStatus.Run:
                case ESimStatus.End:
                    MessageBox.Show("系统在“初始化完成、暂停”状态下才可以执行数据保存!");
                    break;
                case ESimStatus.Pause:
                case ESimStatus.Inited:
                    if (DlgStartSaveThread != null)
                    {
                        DlgStartSaveThread(DataSaveFlagModelList, SelectedDataSaveType, DataSavePath);
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 系统切换事件
        //private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    switch (SelectedNavSysType)
        //    {
        //        case NavSysType.Beidou系统:
        //            DataSaveTypeList.Clear();
        //            DataSaveTypeList.Add(DataSaveType.Rinex3_02格式);
        //            DataSaveTypeList.Add(DataSaveType.自定义格式);
        //            break;
        //        case NavSysType.GPS系统:
        //            DataSaveTypeList.Clear();
        //            DataSaveTypeList.Add(DataSaveType.Rinex3_02格式);
        //            DataSaveTypeList.Add(DataSaveType.Rinex2_10格式);
        //            DataSaveTypeList.Add(DataSaveType.自定义格式);
        //            break;
        //        case NavSysType.Galileo系统:
        //            DataSaveTypeList.Clear();
        //            DataSaveTypeList.Add(DataSaveType.Rinex3_02格式);
        //            DataSaveTypeList.Add(DataSaveType.自定义格式);
        //            break;
        //        case NavSysType.GLONASS系统:
        //            DataSaveTypeList.Clear();
        //            DataSaveTypeList.Add(DataSaveType.Rinex2_10格式);
        //            DataSaveTypeList.Add(DataSaveType.Rinex3_02格式);
        //            DataSaveTypeList.Add(DataSaveType.自定义格式);
        //            break;
        //        default:
        //            DataSaveTypeList.Clear();
        //            DataSaveTypeList.Add(DataSaveType.自定义格式);
        //            break;
        //    }
        //    cbxDataType.SelectedIndex = 0;
        //}
        #endregion

        #region INotifyPropertyChanged接口
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("info"));
            }
        }
        #endregion

        #region 释放资源
        public void ReleaseData()
        {
            //throw new NotImplementedException();
        }
        #endregion

        #region ///窗体控件事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.Description = "请选择一个文件夹来保存仿真数据";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataSavePath = fbd.SelectedPath;
                txtSavePath.Text = fbd.SelectedPath;
                txtSavePath.ToolTip = fbd.SelectedPath;
            }
            //  还原路径
            Directory.SetCurrentDirectory(currentDirectory);
        }
        #endregion

    }
}
